package com.woniuxy.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.business.dao.ActivityDao;
import com.woniuxy.business.service.ActivityService;
import com.woniuxy.commons.entity.Activity;
import com.woniuxy.commons.entity.QueryEntity;
import com.woniuxy.commons.utils.ResStatus;
import com.woniuxy.commons.utils.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 雷珉
 * @date 2022年06月08日 16:30
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Resource
    private ActivityDao activityDao;

    @Override
    public ResponseResult<List<Activity>> findAll() {
        List<Activity> all = activityDao.findAll();
        return new ResponseResult<>(200,"查询成功",all,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<PageInfo<Activity>> findAllByCondition(QueryEntity<Activity> queryEntity) {
        //开启分页
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());
        //排序
        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            //格式化排序字段
            String orderKey = queryEntity.getOrderKey().replace("Time","_time");
            PageHelper.orderBy(orderKey+" "+orderSort);
        }
        //封装数据
        List<Activity> all = activityDao.findAllByCondition(queryEntity.getEntity());
        PageInfo<Activity> pageInfo = PageInfo.of(all);
        return new ResponseResult<>(200,"查询成功",pageInfo,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<Activity> findOne(int id) {
        Activity one = activityDao.findOne(id);
        return new ResponseResult<>(200,"查询成功",one,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult addOne(Activity activity) {
        activityDao.addOne(activity);
        return new ResponseResult<>(200,"添加成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteOne(int id) {
        activityDao.deleteOne(id);
        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteBatch(int[] ids) {
        activityDao.deleteBatch(ids);
        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult updateOne(Activity activity) {
        activityDao.updateOne(activity);
        return new ResponseResult<>(200,"更新成功",null,ResStatus.SUCCESS);
    }
}

